package com.ph.demo.current;

/**
 * 当前对象实例方法加锁
 * 一定是当前对象
 */
public class SynchronizeDemo2 implements Runnable{

    static SynchronizeDemo2 synchronizeDemo1 = new SynchronizeDemo2();

    Object o = new Object();




    static int i = 0;

    public synchronized void increase(){
//        System.out.println(Thread.currentThread().getName()+"-------"+i);
        i++;
    }


    @Override
    public void run() {


        for(int j = 0;j<1000000;j++){
//            System.out.println(j);
            increase();

        }
    }

    public static void main(String[] args) throws Exception{
//        Thread thread = new Thread(new SynchronizeDemo2());
//        Thread thread1 = new Thread(new SynchronizeDemo2());
        Thread thread = new Thread(synchronizeDemo1);
        Thread thread1 = new Thread(synchronizeDemo1);
        thread.start();
        thread1.start();
        thread.join();
        thread1.join();
          //等待线程执行结束
        System.err.println("-----"+i);
    }
}
